//
//  GPResultViewC.m
//  股票盈亏计算器
//
//  Created by apple on 2019/8/12.
//  Copyright © 2019 apple. All rights reserved.
//

#import "GPResultViewC.h"

#define kInPlacehoderArr @[@"元", @"元", @"元", @"%"]


@interface GPResultViewC ()
@property (nonatomic, strong) UIImageView *bgIV;
//bigSV
@property (nonatomic, strong) UIScrollView *bigSV;

@property (nonatomic, strong) GPModel *gpmodel;

@property (nonatomic, strong) UIView *contentView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leftMargin;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *rightMargin;


@property (strong, nonatomic) IBOutletCollection(UITextField) NSArray *outTFArr;

@end

@implementation GPResultViewC

- (instancetype)initWithGPModel:(GPModel *)mdoel {
    self = [self init];
    if (self) {
        _gpmodel = mdoel;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = UIColor.whiteColor;
    
    [self setupSubViews];
    
    //    [self setupSubViews];
    
    
}


- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    //    //2.0
    self.bigSV.frame = CGRectMake(0, 0, hhy_kScreenWidth, hhy_kScreenHeight-hhy_kTabbarSafeBottomMargin-hhy_kStatusBarAndNavigationBarHeight);
    
}

- (void)createNavi {
    [self setupNavi_White];
    self.navigationItem.title = @"计算结果";
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self createNavi];
    
}





- (void)setupSubViews {
    [self.view addSubview:self.bgIV];
    
    [self.view addSubview:self.bigSV];
    
    [self.bigSV addSubview:self.contentView];
    //
    
    if (@available(iOS 11.0, *)) {
        self.bigSV.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    [self updateUIAfterLoadNib];
    
    //隐藏键盘
    UITapGestureRecognizer *tap= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidekeyBoardAction)];
    [self.view addGestureRecognizer:tap];
}

#pragma mark - 创建rightView
- (UIView *)createRightViewWithTitle:(NSString *)title {
    UILabel *leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 28)];
    leftLabel.textColor = UIColor.lightGrayColor;
    leftLabel.font = [UIFont systemFontOfSize:16];
    leftLabel.textAlignment = NSTextAlignmentRight;
    leftLabel.text = title;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 70, 28)];
    [view addSubview:leftLabel];
    return view;
}

- (void)updateUIAfterLoadNib {
    int i = 0;
    
    NSArray *res = @[_gpmodel.result1, _gpmodel.result2, _gpmodel.result3, _gpmodel.result4];
    
    //输入框
    for (UITextField *tf in self.outTFArr) {
        tf.rightViewMode = UITextFieldViewModeAlways;
        tf.rightView = [self createRightViewWithTitle:kInPlacehoderArr[i]];
        tf.text = [NSString stringWithFormat:@"%@", res[i]];
        tf.enabled = NO;
        i++;
    }
    
    if (hhy_kScreenWidth > 375) {
        self.leftMargin.constant = 45.0f;
        self.rightMargin.constant = 45.0f;
    }
    
}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    //    //2.0
    //    if ([string isEqualToString:@"."]&&[textField.text containsString:@"."]) {
    //        return NO;
    //    }
    
    //    //4.0
    //    if ([HHYUtils isEmpty:textField.text] && [string isEqualToString:@"."]) {
    //        return NO;
    //    }
    
    return YES;
}

#pragma mark - getter
- (UIScrollView *)bigSV {
    if (!_bigSV) {
        _bigSV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, hhy_kScreenWidth, hhy_kScreenHeight-hhy_kStatusBarAndNavigationBarHeight-hhy_kTabbarSafeBottomMargin)];
        //        mysssSV.delegate = self;
        //        _bigSV.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        _bigSV.backgroundColor = [UIColor clearColor];
        _bigSV.showsHorizontalScrollIndicator = NO;
        _bigSV.showsVerticalScrollIndicator = NO;
        
        //contentSize
        _bigSV.contentSize = CGSizeMake(0, self.contentView.xl_height);
    }
    return _bigSV;
}


- (UIImageView *)bgIV {
    if (!_bgIV) {
        _bgIV = [[UIImageView alloc] initWithFrame:self.view.bounds];
        _bgIV.image = [UIImage imageNamed:@"bg"];
    }
    return _bgIV;
}

- (UIView *)contentView {
    if (!_contentView) {
        UINib *nib = [UINib nibWithNibName:@"hhy_ResultView" bundle:nil];
        NSArray *objs = [nib instantiateWithOwner:self options:nil];
        _contentView = objs[0];
        _contentView.frame = CGRectMake(0, 0, hhy_kScreenWidth, 500);
    }
    return _contentView;
}

@end
